import {ref} from 'vue'
import problem from '../types/problem' 
import request from '@/base/request'
import Page from '@/base/page'
import {ElMessage} from 'element-plus'
import Prism from 'prismjs';
import ProblemParm from '@/types/problemParm'
export default function(){


    const showProblemDetail=ref<boolean>(false);

    const hideAnswer=ref<boolean>(true)

    const chooseProblem=ref<problem>({
        id:null,
        type:null,
        problemDescribe:null,
        problemCode:null,
        codeLanguage:null,
        collected:false

    });
    const problemList=ref<Page<problem>>({
        items:[],
        counts:0,
        page:0,
        pageSize:0,
    })

    const problemParm=ref<ProblemParm>({
        pageNo:1,
        pageSize:7,
    })

    async function getRandomProblem() {
        const response =  await request.post("/problem/getRandomProblem",problemParm.value)
        if(response.data.code===200){
            chooseProblem.value=response.data.data

            Prism.highlightAll();
            setTimeout(()=>{Prism.highlightAll(); },400)
        }else{
            ElMessage({
                message:`获取题目失败：${response.data.msg}`,
                grouping:true,
                type:'error'
            })
        }
    }

    async function getProblemList() {
        const response =  await request.post("/problem/getProblemList",problemParm.value)
        if(response.data.code===200){
            problemList.value=response.data.data
        }else{
            ElMessage({
                message:`获取题目失败：${response.data.msg}`,
                grouping:true,
                type:'error'
            })
        }
    }

    async function handleRowClick2(row){
        const response=await request.get(`/problem/getProblemById/${row.problemId}`)
        if(response.data.code===200){
            chooseProblem.value=response.data.data
            hideAnswer.value=true
            showProblemDetail.value=true;
        }else{
            ElMessage({
                message:`获取题目详情失败：${response.data.msg}`,
                grouping:true,
                type:'error'
            })
        }
    }


    async function handlePageChange(newPage:number){
        problemParm.value.pageNo=newPage
        await getProblemList()
    }

    function handleDifficultyChange(newDifficulty:number){
        problemParm.value.difficulty=newDifficulty
        getProblemList()
    }

    function handleRowClick(row){
        hideAnswer.value=true
        showProblemDetail.value=true;
        chooseProblem.value=row
    }



    function handleTypeChange(newType:number){
        problemParm.value.type=newType
        getProblemList()
    }



    return {hideAnswer,chooseProblem,showProblemDetail,problemList,problemParm,getRandomProblem,getProblemList,handlePageChange,handleRowClick,handleRowClick2,handleDifficultyChange,handleTypeChange}
}